﻿using System;
using System.Data;
using System.Linq;
using SD.LLBLGen.Pro.QuerySpec;
using SD.LLBLGen.Pro.QuerySpec.Adapter;
using VNet.VisiSuite.BusinessLogic.BusinessException;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.Dal.DatabaseSpecific;
using VNet.VisiSuite.Dal.EntityClasses;
using VNet.VisiSuite.Dal.FactoryClasses;
using VNet.VisiSuite.Dal.HelperClasses;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Contact.Message.Base;
using System.Collections.Generic;
using VNet.VisiSuite.Common.Logger;

namespace VNet.VisiSuite.BusinessLogic
{
    public class ProductionPlanUtil
    {
        private readonly QueryFactory _queryFactory = new QueryFactory();

        public GetDataResponse<IList<ProductionPlanItem>> GetProductionPlans(GetDataRequest request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    //var productionPlanQuery = _queryFactory.ProductionPlan
                    //    .OrderBy(ProductionPlanFields.SortOrder.Ascending());

                    var productionPlanQuery = _queryFactory.Create()
                        .Select(() => new
                        {
                            CellId = ProductionPlanFields.CellId.Source("c").ToValue<int>(),
                            ProductionPlanId = ProductionPlanFields.ProductionPlanId.Source("c").ToValue<int>(),
                            BraiderProductId = ProductionPlanFields.BraiderProductId.Source("c").ToValue<int?>(),
                            ProductId = ProductionPlanFields.ProductId.Source("c").ToValue<int>(),
                            Enabled = ProductionPlanFields.Enabled.Source("c").ToValue<Boolean>(),
                            Actual = ProductionPlanFields.Actual.Source("c").ToValue<int>(),
                            ActualDate = ProductionPlanFields.ActualDate.Source("c").ToValue<DateTime?>(),
                            Closed = ProductionPlanFields.Closed.Source("c").ToValue<Boolean>(),
                            DueDate = ProductionPlanFields.DueDate.Source("c").ToValue<DateTime>(),
                            Planned = ProductionPlanFields.Planned.Source("c").ToValue<int>(),
                            SortOrder = ProductionPlanFields.SortOrder.Source("c").ToValue<int>(),
                            WorkOrder = ProductionPlanFields.WorkOrder.Source("c").ToValue<string>(),
                            OperationId = ProductionPlanFields.OperationId.Source("c").ToValue<int?>(),
                            OperationPass = ProductionPlanFields.OperationPass.Source("c").ToValue<short?>(),
                            StationId = ProductionPlanFields.StationId.Source("c").ToValue<int?>(),
                            LastUpdatedDate = ProductionPlanFields.LastUpdatedDate.Source("c").ToValue<DateTime>(),
                            CreatedDate = ProductionPlanFields.CreatedDate.Source("c").ToValue<DateTime>(),
                            FirstName1 = (UserLoginFields.FirstName.Source("u").SetFieldAlias("FistName1")).ToValue<string>(),
                            LastName1 = (UserLoginFields.LastName.Source("u").SetFieldAlias("LastName1")).ToValue<string>(),
                        })
                        .From(_queryFactory.ProductionPlan.As("c")
                        .InnerJoin(ProductionPlanEntity.Relations.UserLoginEntityUsingLastUpdatedBy, "c", "u")
                        .InnerJoin(ProductionPlanEntity.Relations.CellEntityUsingCellId, "c", "e"))
                        .Where(CellFields.AreaId.Source("e") == request.Id)
                        .OrderBy(CellFields.CellName.Source("e").Ascending(),ProductionPlanFields.SortOrder.Source("c").Ascending());
                    var productionPlanEntities = adapter.FetchQuery(productionPlanQuery);
                    if (productionPlanEntities == null)
                        return new GetDataResponse<IList<ProductionPlanItem>> { Success = false, Error = BusinessLogicError.GetProductionPlansFailed };

                    var result = new List<ProductionPlanItem>();
                    foreach (var productionPlanEntity in productionPlanEntities)
                    {
                        var productionPlanItem = new ProductionPlanItem
                        {
                            CellId = productionPlanEntity.CellId,
                            ProductionPlanId = productionPlanEntity.ProductionPlanId,
                            BraiderProductId = productionPlanEntity.BraiderProductId,
                            ProductId = productionPlanEntity.ProductId,
                            Enabled = productionPlanEntity.Enabled,
                            Actual = productionPlanEntity.Actual,
                            ActualDate = productionPlanEntity.ActualDate,
                            Closed = productionPlanEntity.Closed,
                            DueDate = productionPlanEntity.DueDate,
                            Planned = productionPlanEntity.Planned,
                            SortOrder = productionPlanEntity.SortOrder,
                            WorkOrder = productionPlanEntity.WorkOrder,
                            OperationId = productionPlanEntity.OperationId,
                            OperationPass = productionPlanEntity.OperationPass,
                            StationId = productionPlanEntity.StationId,
                            CreatedByUser = string.Format("{0} {1}", productionPlanEntity.FirstName1, productionPlanEntity.LastName1),
                            LastUpdatedByUser = string.Format("{0} {1}", productionPlanEntity.FirstName1, productionPlanEntity.LastName1),
                            CreatedDate = productionPlanEntity.CreatedDate,
                            LastUpdatedDate = productionPlanEntity.LastUpdatedDate,
                        };
                        result.Add(productionPlanItem);
                    }

                    return new GetDataResponse<IList<ProductionPlanItem>> { Success = true, Data = result };
                }
                catch (Exception ex)
                {
                    FileLog.Error("GetProductionPlans", ex);
                    return new GetDataResponse<IList<ProductionPlanItem>> { Success = false, Error = BusinessLogicError.GetProductionPlansFailed };
                }
            }
        }


        public GetDataResponse<ProductionPlanReferenceItem> GetProductionPlanReference(GetDataRequest request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    var result = new ProductionPlanReferenceItem
                        {
                            Cells = new List<CellItem>(),
                            Products = new List<ProductItem>(),
                            BraiderProducts = new List<BraiderProductItem>()
                        };
                    var cellQuery = _queryFactory.Cell.WithPath(CellEntity.PrefetchPathUserLogin,
                                                                CellEntity.PrefetchPathUserLogin1)
                                                 .Where(CellFields.Enabled == true)
                                                 .AndWhere(CellFields.AreaId == request.Id);
                    var cellEntities = (EntityCollection<CellEntity>) adapter.FetchQuery(cellQuery);
                    if (cellEntities == null)
                        return new GetDataResponse<ProductionPlanReferenceItem>
                            {
                                Success = false,
                                Error = BusinessLogicError.GetProductionPlanReferenceFailed
                            };
                    var productQuery = _queryFactory.Product.WithPath(ProductEntity.PrefetchPathUserLogin,
                                                                      ProductEntity.PrefetchPathUserLogin1)
                                                    .Where(ProductFields.Enabled == true);
                    var productEntities = (EntityCollection<ProductEntity>)adapter.FetchQuery(productQuery);
                    if (productEntities == null)
                        return new GetDataResponse<ProductionPlanReferenceItem>
                        {
                            Success = false,
                            Error = BusinessLogicError.GetProductionPlanReferenceFailed
                        };
                    var braiderProductQuery = _queryFactory.BraiderProduct;
                    var braiderEntities = (EntityCollection<BraiderProductEntity>)adapter.FetchQuery(braiderProductQuery);
                    if (braiderEntities == null)
                        return new GetDataResponse<ProductionPlanReferenceItem>
                        {
                            Success = false,
                            Error = BusinessLogicError.GetProductionPlanReferenceFailed
                        };
                    foreach (var cellEntity in cellEntities.OrderBy(t => t.CellName))
                    {
                        var cell = new CellItem
                            {
                                CellId = cellEntity.CellId,
                                AreaId = cellEntity.AreaId,
                                CellName = cellEntity.CellName,
                                Description = cellEntity.Description,
                                Enabled = cellEntity.Enabled,
                                CreatedBy =
                                    string.Format("{0} {1}", cellEntity.UserLogin.FirstName,
                                                  cellEntity.UserLogin.LastName),
                                LastUpdatedBy =
                                    string.Format("{0} {1}", cellEntity.UserLogin1.FirstName,
                                                  cellEntity.UserLogin1.LastName),
                                CreatedDate = cellEntity.CreatedDate,
                                LastUpdatedDate = cellEntity.LastUpdatedDate,
                            };
                        result.Cells.Add(cell);
                    }

                    foreach (var productEntity in productEntities.OrderBy(t => t.Sku))
                    {
                        var productItem = new ProductItem
                        {
                            ProductId = productEntity.ProductId,
                            Sku = productEntity.Sku,
                            Description = productEntity.Description,
                            Revision = productEntity.Revision,
/**
                            PartTime = productEntity.PartTime,
                            SetupTime = productEntity.SetupTime,
                            CycleTime = productEntity.CycleTime,
                            LoadUnloadTime = productEntity.LoadUnloadTime,
 **/
                            Enabled = productEntity.Enabled,
                            CreatedDate = productEntity.CreatedDate,
                            LastUpdatedDate = productEntity.LastUpdatedDate,
                            CreatedByUser = string.Format("{0} {1}", productEntity.UserLogin.FirstName, productEntity.UserLogin.LastName),
                            LastUpdatedByUser = string.Format("{0} {1}", productEntity.UserLogin1.FirstName, productEntity.UserLogin1.LastName),
                        };
                        result.Products.Add(productItem);
                    }


                    foreach (var braiderProductEntity in braiderEntities.OrderBy(t => t.BraiderNumber))
                    {
                        var item = new BraiderProductItem
                        {
                            BraiderNumber = braiderProductEntity.BraiderNumber,
                            BraiderProductId = braiderProductEntity.BraiderProductId,
                            BraiderType = braiderProductEntity.BraiderType,
                            CarrierCount = braiderProductEntity.CarrierCount,
                            ClientId = braiderProductEntity.ClientId,
                            DeckCount = braiderProductEntity.DeckCount,
                            FeetPerMinute = braiderProductEntity.FeetPerMinute,
                            HoseCount = braiderProductEntity.HoseCount,
                            HoseSize = braiderProductEntity.HoseSize,
                            HoseSpec = braiderProductEntity.HoseSpec,
                            OperatorId = braiderProductEntity.OperatorId,
                            Split = braiderProductEntity.Split,
                        };
                        result.BraiderProducts.Add(item);
                    }

                    return new GetDataResponse<ProductionPlanReferenceItem> { Success = true, Data = result };
                }
                catch (Exception ex)
                {
                    FileLog.Error("GetProductionPlanReference", ex);
                    return new GetDataResponse<ProductionPlanReferenceItem>
                    {
                        Success = false,
                        Error = BusinessLogicError.GetProductionPlanReferenceFailed
                    };
                }
            }
        }

        public SaveDataResponse<ProductionPlanItem> UpdateProductionPlan(SaveDataRequest<ProductionPlanItem> request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    var query = _queryFactory.ProductionPlan.Where(ProductionPlanFields.ProductionPlanId == request.Data.ProductionPlanId);

                    var productionPlanEntity = adapter.FetchFirst(query);
                    if (productionPlanEntity == null)
                        return new SaveDataResponse<ProductionPlanItem> { Error = BusinessLogicError.ProductionPlanDoesNotExist, Success = false };
                    var message = CheckWorkOrderNameUnique(adapter, request.Data);
                    if (!string.IsNullOrEmpty(message))
                    {
                        return new SaveDataResponse<ProductionPlanItem>
                        {
                            Error = BusinessLogicError.WorkOrderExist,
                            Success = false
                        };
                    }

                    productionPlanEntity.CellId = request.Data.CellId;
                    productionPlanEntity.ProductionPlanId = request.Data.ProductionPlanId;
                    productionPlanEntity.BraiderProductId = request.Data.BraiderProductId;
                    productionPlanEntity.ProductId = request.Data.ProductId;
                    productionPlanEntity.Enabled = request.Data.Enabled;
                    productionPlanEntity.Actual = request.Data.Actual;
                    productionPlanEntity.Closed = request.Data.Closed;
                    productionPlanEntity.DueDate = request.Data.DueDate;
                    productionPlanEntity.Planned = request.Data.Planned;
                    productionPlanEntity.SortOrder = request.Data.SortOrder;
                    productionPlanEntity.WorkOrder = request.Data.WorkOrder;
                    productionPlanEntity.LastUpdatedBy = request.Identity.UserLoginId;
                    productionPlanEntity.LastUpdatedDate = DateTime.UtcNow;

                    var result = adapter.SaveEntity(productionPlanEntity, true);

                    if (!result)
                        return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.UpdateUserFailed };

                    // Get production plan entity after updated
                    var q = _queryFactory.Create()
                        .Select(() => new
                        {
                            CellId = ProductionPlanFields.CellId.Source("c").ToValue<int>(),
                            ProductionPlanId = ProductionPlanFields.ProductionPlanId.Source("c").ToValue<int>(),
                            BraiderProductId = ProductionPlanFields.BraiderProductId.Source("c").ToValue<int?>(),
                            ProductId = ProductionPlanFields.ProductId.Source("c").ToValue<int>(),
                            Enabled = ProductionPlanFields.Enabled.Source("c").ToValue<Boolean>(),
                            Actual = ProductionPlanFields.Actual.Source("c").ToValue<int>(),
                            Closed = ProductionPlanFields.Closed.Source("c").ToValue<Boolean>(),
                            DueDate = ProductionPlanFields.DueDate.Source("c").ToValue<DateTime>(),
                            Planned = ProductionPlanFields.Planned.Source("c").ToValue<int>(),
                            SortOrder = ProductionPlanFields.SortOrder.Source("c").ToValue<int>(),
                            WorkOrder = ProductionPlanFields.WorkOrder.Source("c").ToValue<string>(),
                            LastUpdatedDate = ProductionPlanFields.LastUpdatedDate.Source("c").ToValue<DateTime>(),
                            CreatedDate = ProductionPlanFields.CreatedDate.Source("c").ToValue<DateTime>(),
                            FirstName1 = (UserLoginFields.FirstName.Source("u").SetFieldAlias("FistName1")).ToValue<string>(),
                            LastName1 = (UserLoginFields.LastName.Source("u").SetFieldAlias("LastName1")).ToValue<string>(),
                        })
                        .From(_queryFactory.ProductionPlan.As("c").InnerJoin(ProductionPlanEntity.Relations.UserLoginEntityUsingLastUpdatedBy, "c", "u"))
                        .Where(ProductionPlanFields.WorkOrder.Source("c") == productionPlanEntity.WorkOrder);

                    var val = adapter.FetchQuery(q).FirstOrDefault();
                    if (val == null)
                        return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.UpdateProductionPlanFailed };

                    return new SaveDataResponse<ProductionPlanItem>
                    {
                        Success = true,
                        Data = new ProductionPlanItem
                        {
                            CellId = val.CellId,
                            ProductionPlanId = val.ProductionPlanId,
                            BraiderProductId = val.BraiderProductId,
                            ProductId = val.ProductId,
                            Enabled = val.Enabled,
                            Actual = val.Actual,
                            Closed = val.Closed,
                            DueDate = val.DueDate,
                            Planned = val.Planned,
                            SortOrder = val.SortOrder,
                            WorkOrder = val.WorkOrder,
                            LastUpdatedByUser = string.Format(FormatString.NameFormat, val.FirstName1, val.LastName1),
                            LastUpdatedDate = val.LastUpdatedDate,
                        }
                    };
                }
                catch (Exception ex)
                {
                    FileLog.Error("UpdateProduct", ex);
                    return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.UpdateProductionPlanFailed };
                }
            }
        }

        public SaveDataResponse<ProductionPlanItem> InsertProductionPlan(SaveDataRequest<ProductionPlanItem> request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    if (!string.IsNullOrEmpty(CheckWorkOrderNameUnique(adapter, request.Data)))
                        return new SaveDataResponse<ProductionPlanItem> { Error = BusinessLogicError.WorkOrderExist, Success = false };

                    DateTime dateTimeNow = DateTime.UtcNow;
                    var productionPlanEntity = new ProductionPlanEntity
                    {
                        CellId = request.Data.CellId,
                        ProductionPlanId = request.Data.ProductionPlanId,
                        BraiderProductId = request.Data.BraiderProductId,
                        ProductId = request.Data.ProductId,
                        Enabled = request.Data.Enabled,
                        Actual = request.Data.Actual,
                        Closed = request.Data.Closed,
                        DueDate = request.Data.DueDate,
                        Planned = request.Data.Planned,
                        SortOrder = request.Data.SortOrder,
                        WorkOrder = request.Data.WorkOrder,
                        LastUpdatedBy = request.Identity.UserLoginId,
                        LastUpdatedDate = dateTimeNow,
                        CreatedBy = request.Identity.UserLoginId,
                        CreatedDate = dateTimeNow,
                    };

                    bool result = adapter.SaveEntity(productionPlanEntity, true);
                    if (!result)
                        return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.InsertProductionPlanFailed };

                    // Get production plan entity after updated

                    var q = _queryFactory.Create()
                        .Select(() => new
                        {
                            CellId = ProductionPlanFields.CellId.Source("c").ToValue<int>(),
                            ProductionPlanId = ProductionPlanFields.ProductionPlanId.Source("c").ToValue<int>(),
                            BraiderProductId = ProductionPlanFields.BraiderProductId.Source("c").ToValue<int?>(),
                            ProductId = ProductionPlanFields.ProductId.Source("c").ToValue<int>(),
                            Enabled = ProductionPlanFields.Enabled.Source("c").ToValue<Boolean>(),
                            Actual = ProductionPlanFields.Actual.Source("c").ToValue<int>(),
                            Closed = ProductionPlanFields.Closed.Source("c").ToValue<Boolean>(),
                            DueDate = ProductionPlanFields.DueDate.Source("c").ToValue<DateTime>(),
                            Planned = ProductionPlanFields.Planned.Source("c").ToValue<int>(),
                            SortOrder = ProductionPlanFields.SortOrder.Source("c").ToValue<int>(),
                            WorkOrder = ProductionPlanFields.WorkOrder.Source("c").ToValue<string>(),
                            LastUpdatedDate = ProductionPlanFields.LastUpdatedDate.Source("c").ToValue<DateTime>(),
                            CreatedDate = ProductionPlanFields.CreatedDate.Source("c").ToValue<DateTime>(),
                            FirstName1 = (UserLoginFields.FirstName.Source("u").SetFieldAlias("FistName1")).ToValue<string>(),
                            LastName1 = (UserLoginFields.LastName.Source("u").SetFieldAlias("LastName1")).ToValue<string>(),
                        })
                        .From(_queryFactory.Product.As("c").InnerJoin(ProductionPlanEntity.Relations.UserLoginEntityUsingLastUpdatedBy, "c", "u"))
                        .Where(ProductionPlanFields.WorkOrder.Source("c") == productionPlanEntity.WorkOrder);

                    var ret = adapter.FetchQuery(q);
                    if (ret.Any())
                    {
                        string user = string.Format(FormatString.NameFormat, ret[0].FirstName1, ret[0].LastName1);
                        return new SaveDataResponse<ProductionPlanItem>
                        {
                            Success = true,
                            Data = new ProductionPlanItem
                            {
                                CellId = ret[0].CellId,
                                ProductionPlanId = ret[0].ProductionPlanId,
                                BraiderProductId = ret[0].BraiderProductId,
                                ProductId = ret[0].ProductId,
                                Enabled = ret[0].Enabled,
                                Actual = ret[0].Actual,
                                Closed = ret[0].Closed,
                                DueDate = ret[0].DueDate,
                                Planned = ret[0].Planned,
                                SortOrder = ret[0].SortOrder,
                                WorkOrder = ret[0].WorkOrder,
                                LastUpdatedByUser = user,
                                LastUpdatedDate = ret[0].LastUpdatedDate,
                                CreatedDate = ret[0].CreatedDate,
                                CreatedByUser = user,
                            }
                        };
                    }
                    return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.InsertProductionPlanFailed };
                }
                catch (Exception ex)
                {
                    FileLog.Error("InsertProductionPlan", ex);
                    return new SaveDataResponse<ProductionPlanItem> { Success = false, Error = BusinessLogicError.InsertProductionPlanFailed };
                }
            }
        }

        public SaveDataResponse<IList<ProductionPlanItem>> ImportProductionPlans(SaveDataRequest<IList<ProductionPlanItem>> request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    adapter.StartTransaction(IsolationLevel.ReadCommitted, "ImportProductionPlans");
                    var items = request.Data;
                    var query =
                        _queryFactory.ProductionPlan;

                    var entities = (EntityCollection<ProductionPlanEntity>)adapter.FetchQuery(query);
                    var cellQuery = _queryFactory.Cell;
                    var cellEntities = (EntityCollection<CellEntity>) adapter.FetchQuery(cellQuery);
                    var productQuery = _queryFactory.Product;
                    var productEntities = (EntityCollection<ProductEntity>)adapter.FetchQuery(productQuery);

                    foreach (var modelItem in items)
                    {
                        var productId = (from productEntity in productEntities where productEntity.Sku == modelItem.Sku select productEntity.ProductId).FirstOrDefault();
                        var cellId = (from cellEntity in cellEntities where cellEntity.CellName == modelItem.CellName select cellEntity.CellId).FirstOrDefault();
                        //Update Production Plan
                        if (entities.Any(x => x.WorkOrder == modelItem.WorkOrder))
                        {
                            var productionPlanEntity = entities.FirstOrDefault(x => x.WorkOrder == modelItem.WorkOrder);
                            if (productionPlanEntity != null)
                            {
                                productionPlanEntity.Planned = modelItem.Planned;
                                productionPlanEntity.DueDate = modelItem.DueDate;
                                productionPlanEntity.Enabled = modelItem.Enabled;
                                productionPlanEntity.SortOrder = modelItem.SortOrder;
                                productionPlanEntity.Actual = modelItem.Actual;
                                productionPlanEntity.Closed = modelItem.Closed;
                                productionPlanEntity.ProductId = productId;
                                productionPlanEntity.CellId = cellId;
                                productionPlanEntity.LastUpdatedDate = DateTime.UtcNow;
                                productionPlanEntity.LastUpdatedBy = request.Identity.UserLoginId;
                            }
                        }
                        else
                        {
                            var newProductionPlan = new ProductionPlanEntity
                            {
                                CellId = cellId,
                                ProductId = productId,
                                Planned = modelItem.Planned,
                                WorkOrder = modelItem.WorkOrder,
                                DueDate = modelItem.DueDate,
                                Enabled = modelItem.Enabled,
                                SortOrder = modelItem.SortOrder,
                                Actual = modelItem.Actual,
                                Closed = modelItem.Closed,
                                LastUpdatedDate = DateTime.UtcNow,
                                LastUpdatedBy = request.Identity.UserLoginId,
                                CreatedDate = DateTime.UtcNow,
                                CreatedBy = request.Identity.UserLoginId,
                            };
                            entities.Add(newProductionPlan);
                        }
                    }
                    adapter.SaveEntityCollection(entities, true, true);
                    adapter.Commit();
                    return new SaveDataResponse<IList<ProductionPlanItem>>
                    {
                        Data = null,
                        Success = true
                    };
                }
                catch (Exception ex)
                {
                    adapter.Rollback();
                    FileLog.Error("ImportProductionPlans", ex);
                    return new SaveDataResponse<IList<ProductionPlanItem>>
                    {
                        Data = null,
                        Error = BusinessLogicError.ImportProductionPlansFailed,
                        Success = false
                    };
                }
            }
        }

        public DeleteDataResponse DeleteProductionPlan(DeleteDataRequest request)
        {
            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    var query2 = _queryFactory.ProductionPlan.Where(ProductionPlanFields.ProductionPlanId == request.DataId);
                    var areaEntity = adapter.FetchFirst(query2);
                    if (areaEntity == null)
                        return new DeleteDataResponse { Error = BusinessLogicError.ProductionPlanDoesNotExist, Success = false };

                    if (!adapter.DeleteEntity(areaEntity))
                        return new DeleteDataResponse { Error = BusinessLogicError.DeleteProductionPlanFailed, Success = false };

                    return new DeleteDataResponse { Success = true, DataId = request.DataId };
                }
                catch
                {
                    return new DeleteDataResponse { Success = false, Error = BusinessLogicError.DeleteProductionPlanFailed };
                }
            }
        }

        private string CheckWorkOrderNameUnique(DataAccessAdapter adapter, ProductionPlanItem data)
        {
            var query = _queryFactory.ProductionPlan.Where(ProductionPlanFields.WorkOrder == data.WorkOrder & ProductionPlanFields.ProductionPlanId != data.ProductionPlanId);
            var productionPlanEntities = (EntityCollection<ProductionPlanEntity>)adapter.FetchQuery(query);

            if (productionPlanEntities != null && productionPlanEntities.Count > 0)
                return ErrorMessages.WorkOrderExist;
            return null;
        }

    }
}
